package com.ece.beans;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.naming.InitialContext;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.ece.beans.items.Item;
import com.ece.beans.users.Adresse;
import com.ece.beans.users.Utilisateur;
import com.ece.persistence.PersistenceUtil;

@Entity
@Table(name="COMMANDE")
public class Commande implements java.io.Serializable {

	@Id
	//	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name="COMMANDE_REF")
	private  String reference;
	@ManyToOne
	@JoinColumn(name="COMMANDE_UTILISATEUR")
	private Utilisateur user;
	@ManyToOne
	@JoinColumn(name="COMMANDE_ADRESSE")
	private Adresse adresse;
	@ManyToMany
	@JoinTable(name="ITEM_COMMANDE")
	private List<Item> items;
	@Column(name="COMMANDE_DATE")
	private Date date;

	public Commande() {
		user = null;
		this.date = new Date();
	}

	public Commande(Utilisateur user) {
		this.user = user;
		setItems(new ArrayList<Item>());
		this.date = new Date();
	}

	public void setReference(String reference) {
		this.reference = reference;
	}

	public String getReference() {
		return reference;
	}

	public Utilisateur getUser() {
		return user;
	}

	public void setUser(Utilisateur user) {
		this.user = user;
	}

	public void setAdresse(Adresse adresse) {
		this.adresse = adresse;
	}

	public Adresse getAdresse() {
		return adresse;
	}

	public void setItems(List<Item> items) {
		this.items = items;
	}

	public List<Item> getItems() {
		return items;
	}

	public void addItem(Item item) {
		this.items.add(item);
	}

	public void removeItem(Item item) {
		if(this.items.contains(item))
			this.items.remove(item);
	}

	public Date getDate() {
		return this.date;
	}

	public void validerCommande() throws Exception{
		this.date = new Date();
		this.reference = this.user.getEmail() + this.date.hashCode();

		PersistenceUtil.init();
		EntityManager em = PersistenceUtil.getEM();
		try {
			PersistenceUtil.beginTransaction();
			em.persist(this);
			PersistenceUtil.commitTransaction();
		}
		finally { PersistenceUtil.closeEM(); }

		InitialContext ic = new InitialContext();
		ConnectionFactory cf = (ConnectionFactory)ic.lookup("ConnectionFactory");
		Destination queue = (Destination)ic.lookup("queue/MyTestQueue");
		Connection con = cf.createConnection();
		try {
			Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
			MessageProducer producer = session.createProducer(queue);
			con.start();
			ObjectMessage message = session.createObjectMessage();
			message.setObject(this);
			producer.send(message);
		} finally {
			con.close();
		}
	}

	@Override
	public String toString() {
		String result = "";
		result += "Commande du " + date.getDate() + "/" + date.getMonth() + "/" + date.getYear() + "\n";
		result += "A : " + this.user.getPrenom() + " " + this.user.getNom() + "\n";
		//		result += "Coordonnees : " + this.adresse.toString() + "\n\n";
		result += "Nombre d'objets : " + this.items.size() + "\n";
		for(Item it : this.items)
			result += it.toString() + "\n";

		return result;

	}

}
